<?php

/***************************************************************\
* FILE:		sys/pages/mail.inc
* SECTION:	pages > mailing system
* FUNCTION:	display a mail message
* USES:	
* INCLUDES:	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
	if (!$_SESSION['user_id'])
		errorpage(6);
}


/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE;

	if ($_GET['out']) // outbox
		$sql = (db_query_select(array(array(), "MAIL_HEADER.*, MAIL_BODY.*, '{$_SESSION['member']['nick']}' AS sender_nick"), array("(((MAIL_HEADER INNER JOIN MAIL_BODY ON MAIL_HEADER.mail_id = MAIL_BODY.mail_id)) INNER JOIN MEMBER AS M1 ON M1.user_id = MAIL_HEADER.sender)", "DUMMY"), "sender = {$_SESSION['user_id']} AND !MAIL_HEADER.deleted AND MAIL_HEADER.mail_id = {$PAGE['id']}"));
	else // inbox
		$sql = (db_query_select(array(array(), "MAIL_HEADER.*, MAIL_BODY.*, MAIL_TO.user_id, MAIL_TO.hasread AS hasread_to, M1.nick AS sender_nick"), array("(((MAIL_HEADER INNER JOIN MAIL_BODY ON MAIL_HEADER.mail_id = MAIL_BODY.mail_id) INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER AS M1 ON M1.user_id = MAIL_HEADER.sender)", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND !MAIL_TO.deleted AND MAIL_HEADER.mail_id = {$PAGE['id']}"));
	
	$sqlresult = db_do_query($sql);
	if (db_num_rows($sqlresult) != 1)
		errorpage(10);
	
	$mail = db_fetch_array($sqlresult);

	// mark mail as read
	if ($_GET['out'] and !$mail['hasread'] and $_SESSION['member']['pref_auto_mark_read'])
		db_do_query(db_query_update(array('sent', 'hasread'), array('sent', 1), "MAIL_HEADER", "mail_id = {$PAGE['id']}"));
	if (!$_GET['out'] and !$mail['hasread_to'] and $_SESSION['member']['pref_auto_mark_read'])
		db_do_query(db_query_update(array('hasread'), array(1), "MAIL_TO", "mail_id = {$PAGE['id']} AND user_id = {$_SESSION['user_id']}"));
		
	
	// mailbox browsing actions
	if (!$_GET['out'] and !$_GET['trash']) { // inbox
		$CONTENT['title'] = "INBOX: {$_SESSION['member']['nick']}";
	} else if (!$_GET['out'] and $_GET['trash']) { // inbox > trash
		$CONTENT['title'] = "INBOX [prullenbak]: {$_SESSION['member']['nick']}";
	} else if ($_GET['out'] and !$_GET['trash']) { // outbox
		$CONTENT['title'] = "OUTBOX: {$_SESSION['member']['nick']}";
	} else if ($_GET['out'] and $_GET['trash']) { // outbox > trashed
		$CONTENT['title'] = "OUTBOX [prullenbak]: {$_SESSION['member']['nick']}";
	}

	add_action('inbox', 'url', '.mailbox');
	add_action('outbox', 'url', '.mailbox?out=1');
	add_action('inbox-t', 'url', '.mailbox?trash=1');
	add_action('outbox-t', 'url', '.mailbox?out=1&trash=1');
	
	// mail actions	
	add_action('mail-reply', 'javascript', '', 'mail_reply();', 1);
	add_action('mail-replyall', 'javascript', '', 'mail_reply_all();', 1);
	add_action('mail-forward', 'javascript', '', 'mail_forward();', 1);
	add_action('mail-unread', 'javascript', '', 'document.unreadform.submit()');
	if ($_GET['trash'])
		add_action('mail-untrash', 'javascript', '', 'document.untrashform.submit()');
	else
		add_action('mail-trash', 'javascript', '', 'mail_trash();');
	add_action('mail-delete', 'javascript', '', 'mail_delete();');

	// retrieve adressees
	$sqlresult = db_do_query(db_query_select(array(array(), "MEMBER.nick, MAIL_TO.cc, MAIL_TO.user_id"), array("MAIL_TO INNER JOIN MEMBER ON MAIL_TO.user_id = MEMBER.user_id", "DUMMY"), "mail_id = {$PAGE['id']}", "cc ASC"));
	$mail['to'] = array();
	$mail['cc'] = array();
	while ($to = db_fetch_array($sqlresult))
		if ($to['cc'])
			$mail['cc'][] = array('user_id' => $to['user_id'], 'nick' => $to['nick']);
		else
			$mail['to'][] = array('user_id' => $to['user_id'], 'nick' => $to['nick']);

	$PAGE['content']['mail'] = $mail;
	$PAGE['content']['title'] = "Bericht #{$mail['mail_id']}";
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_mail($PAGE['content']['mail'], $_GET['out'], $_GET['trash']);
	print_form($PAGE['content']['deleteform']);
}

?>